<!DOCTYPE html>
<html>
<head>
    <#import "../common/common.macro.ftl" as netCommon>
    <title>${title!"艺术上海"}</title>
    <meta charset="utf-8" />
    <meta name="keywords" content="${keywords?if_exists}" />
    <meta name="description" content="${description?if_exists}" />
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <link href="/static/css/bootstrap.mini.css" rel="stylesheet" />
    <link href="/static/css/style.css" rel="stylesheet" />
    <link href="/static/css/common/index.css" rel="stylesheet" />

    <link href="/static/css/footer/index.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link rel="icon" type="image/png" href="/static/img/icon.png"/>

    <style>
        .container-p{
            width: 1200px;
        }
        .padding-y{
            margin: 20px;
        }
        .form-group label{
            margin: 10px 0px;
        }

        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            padding: 0.75rem;
            vertical-align: middle;
            border-top: 1px solid #dee2e6;
        }

    </style>
</head>
<body>
<@netCommon.commonHeader "首页" menu i18n/>
<!-- header -->
<#--<@netCommon.commonHeaderNav "works" menu i18n/>-->

<section class="section-pagetop bg" style="margin-top: 110px">
    <div class="container container-p">
        <h2 class="title-page">我的收货地址</h2>
    </div> <!-- container //  -->
</section>

<section class="section-content padding-y ">
    <div class="container container-p">

        <!-- =========================  COMPONENT MYORDER 1 ========================= -->
        <div class="row">
            <aside class="col-md-3">
                <!--   SIDEBAR   -->
                <ul class="list-group">
                    <a class="list-group-item " href="/user/order"> 我的订单 </a>
                    <a class="list-group-item" href="/user/setting"> 个人设置 </a>
                    <a class="list-group-item active" href="/user/address"> 我的收货地址 </a>
                    <a class="list-group-item" href="/user/history"> 足迹 </a>
                </ul>


            </aside>
            <main class="col-md-9">

                <article class="card">
                    <div class="card-body">
                        <h4 class="card-title mb-4" style="margin-bottom: 20px">我的收货地址</h4>
                        <button class="btn btn-primary  btn-sm" id="btn-add" style="margin-bottom: 10px;">新增</button>
                        <table class="table table-borderless table-shopping-cart">
                            <thead class="text-muted">
                            <tr class="small text-uppercase">
                                <th scope="col" width="120">选择</th>
                                <th scope="col" width="120">收件人</th>
                                <th scope="col" width="120">电话</th>
                                <th scope="col" width="">地址</th>
                                <th scope="col" class="text-center" width="220">操作</th>
                            </tr>
                            </thead>
                            <tbody id="address-list">


                            </tbody>
                        </table>
                    </div> <!-- card-body .// -->

                </article> <!-- order-group.// -->
            </main>
        </div> <!-- row.// -->
    </div>
</section>
        <!-- footer -->


<!-- script -->
<@netCommon.commonScript />


<script>
    var list ;//收货地址

    $(function () {
        queryAddressList();
        $("#btn-add").click(function () {
            window.location.href = '/user/addressDetail?mode=add'
        })
    })


    function queryAddressList() {


        $.ajax({
            type: "GET",
            url: baseUrl+"api/user-center/app/cust/addr/list",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                'lbv': '1.0',
                'tenant' : 'PTC',
                'token':  getToken(),
            },
            success: function (res) {
                if(res && res.code && res.code=='0' ){
                    if(res.data){
                        //有地址
                        list = res.data;

                        for(var i=0; i<list.length; i++){
                           // var html = '  <p>'+list[i].linkManName +' '+list[i].linkManMobile+'</p> <p>'+list[i].addr+'</p>'
                            var html = '<tr style="height: 60px" class="addr-'+list[i].id+'" data-id="+i+"><td><a href="#"  class="btn btn-sm btn-primary btn-sel"  data-index="'+i+'" data-id="'+list[i].id+'">选择</a></td><td>'+list[i].linkManName +'</td> <td>'+list[i].linkManMobile+'</td> <td>'+list[i].addr+'</td><td><a href="#" class="btn btn-light  btn-edit" data-id="'+list[i].id+'"> 编辑 </a> <a href="#" class="btn btn-light  btn-del" data-id="'+list[i].id+'"> 删除 </a></td></tr> '
                            $('#address-list').append(html)

                        }

                    }else{
                        //没有地址
                        $('#address-list').html('<a href="#" class="btn btn-primary btn-sm" id="btn-add-address " style="margin-top: 20px;"> 新建地址</a>')
                        $('#btn-add-address').click(function () {
                            alert('新增地址');
                        })
                    }

                    $(".btn-sel").click(function () {
                        var selid= $(this).attr('data-id');
                        var index =  $(this).attr('data-index');
                        var currAddr = list[index];
                        localStorage.setItem('currAddr',JSON.stringify(currAddr))
                        window.history.back(-1);
                    })
                    $(".btn-edit").click(function () {
                        var selid= $(this).attr('data-id')
                        window.location.href='/user/addressDetail?id='+selid;
                    })
                    $(".btn-del").click(function () {
                        var selid= $(this).attr('data-id')
                        var r=confirm("是否要删除");
                        if (r==true){
                            $.ajax({
                                type: "GET",
                                url: baseUrl+"api/user-center/app/cust/addr/delete?id="+selid,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                headers: {
                                    'lbv': '1.0',
                                    'tenant' : 'PTC',
                                    'token':  getToken(),
                                },
                                success: function (res) {
                                    if(res && res.code && res.code=='0' ){
                                       alert('删除成功')
                                        $(".addr-"+selid).remove();
                                    }
                                },
                                error: function (res) {

                                }
                            });
                        }

                    })
                }else if(res.code =='-3'){
                    var url = window.location.href
                    debugger;
                    relogin(url);
                }
            },
            error: function (res) {

            }
        });
    }

</script>



</body>
</html>
